import { config } from '../../utils/config'
import { HTTP } from '../../utils/http.js'
import api from '../../utils/api.js'
const app = getApp();
let http = new HTTP()
Page({
    data: {
        tab: 0,
        showIndex: 0,
        imgUrl: config.imgUrl,
        leftegg: 0,
        rightegg: 0,
        xuniegg: 0,
        leftLIjin: 1,
        rigthLijin: 1,
        isShow: false,
        isnone: false,
        isnonetwo: false,
        imgBottom: config.imgUrl + '/images/xiangyou2.png',
        imgRight: config.imgUrl + '/images/xiangyou.png',
        idMyFriend: '',
        isViewContent: false,
        isViewContent2: false,
        eggDialog: {
            show: false,
            showCancel: false,
            showConfirm: true,
            toggle: function () {
                this.show = !this.show;
                return this;
            },
        },
        dailiDialog: {
            show: false,
            showCancel: false,
            showConfirm: true,
            toggle: function () {
                this.show = !this.show;
                return this;
            },
        },
    },
    eggDialogToggle: function (e) {
        this.setData({
            eggDialog: this.data.eggDialog.toggle()
        });
    },
    dailiDialogToggle: function (e) {
        this.setData({
            dailiDialog: this.data.dailiDialog.toggle()
        });
    },
    onLoad: function (options) {
        this.getFriend()
        this.getFriendDaili()
        this.setData({
            tab: options.tab
        })
    },
    //发模板消息
    remindFriend(event) {
        let fid = event.currentTarget.dataset.fid
        http.request({
            url: api.onRemind,
            method: 'get',
            header: {
                token: wx.getStorageSync('token')
            },
            data: {
                friend_id: fid
            },
            success: function (res) {
                wx.showToast({
                    title: '提醒好友成功',
                });
            }
        })


    },
    toDaliList(){ //跳转代理名单
        wx.navigateTo({
          url: '/pages/dailiList/dailiList',
        })
    },
    //获取好友列表领鸡蛋的
    getFriend() {
        var that = this
        http.request({
            url: api.friendEgg,
            method: 'get',
            header: {
                token: wx.getStorageSync('token')
            },
            success: function (res) {
                console.log(res)
                if (res.code == 200) {
                    that.setData({
                        leftegg: res.data.collectable_egg_num,
                        rightegg: res.data.already_egg_num,
                        xuniegg: res.data.fictitious_egg_num,
                        friendsList: res.data.friends,
                    })
                    if (res.data.friends.length == 0) {
                        //就不显示下面
                        that.setData({
                            isShow: false
                        })
                    } else if (res.data.friends.length > 0) {
                        let friendsList = res.data.friends;
                        friendsList.forEach(item => {
                            item.friendShow = false
                        })

                        that.setData({
                            isShow: true
                        })
                    }
                    if (Array.isArray(res.data.l) == 0) {
                        that.setData({
                            isnonetwo: false
                        })
                    } else {
                        that.setData({
                            isnonetwo: true
                        })
                    }
                    if (Array.isArray(res.data.r) == 0) {
                        that.setData({
                            isnone: false
                        })
                    } else {
                        that.setData({
                            isnone: true
                        })
                    }
                }
            }

        })
    },
    isShowview() {
        this.setData({
            isViewContent: !this.data.isViewContent
        })
    },
    isShowview2() {
        this.setData({
            isViewContent2: !this.data.isViewContent2
        })
    },
    //获取好友做代理的
    getFriendDaili() {
        var that = this
        http.request({
            url: api.friendDaiLi,
            method: 'post',
            header: {
                token: wx.getStorageSync('token')
            },
            success: function (res) {
                console.log(res)
                if (res.code == 200) {
                    that.setData({
                        leftLIjin: res.data.real_cashgift,
                        rigthLijin: res.data.fic_cashgift,
                        chengweiList: res.data.r,
                        isnonetwoList: res.data.l
                    })
                }
            }
        })
    },
    tab_click: function (e) { //点击切换
        console.log(e.target.dataset.current)
        var that = this;
        console.log(that.data.tab)
        if (that.data.tab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                tab: e.target.dataset.current
            })
        }
    },
    panel: function (e) {
        console.log(e)
        if (e.currentTarget.dataset.index != this.data.showIndex) {
            this.setData({
                showIndex: e.currentTarget.dataset.index
            })
        } else {
            this.setData({
                showIndex: 0
            })
        }
    },
    //获取用户邀请码
    reqCode() {
        http.request({
            url: '/client/getInviteCode',
            header: {
                'token': wx.getStorageSync("token")
            },
            success: res => {
                console.log(res.data.invite_code)
                try {
                    wx.setStorageSync('invcode', res.data.invite_code)
                } catch (e) { }
            }
        })
    },
    onShow: function () {
        this.reqCode()
    },

    onShareAppMessage: function (res) {
        console.log(wx.getStorageSync('invcode'))
        let url = '/pages/vippromotion/vippromotion?code=' + wx.getStorageSync('invcode');
        return {
            title: '我每天都按味歌推荐的菜谱下单买菜，帮我打开助力得…',
            path: '/pages/index/index?GoTo=' + encodeURIComponent(url),
            // imageUrl: '../../images/vip_share.jpg',
            imageUrl: '../../images/20200808175233.jpg',
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: "分享成功",
                    icon: 'success',
                    duration: 1500
                })
            },
            fail: function (res) {
                // 转发失败
                wx.showToast({
                    title: "分享失败",
                    duration: 2001
                })
            }
        }
    },

})